package com.klikmee.sms.core.base.communicator.starion;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.HttpRequestHandler;

/**
 * Implementation of HTTP request handler used to handle starion's bulk message
 * delivery report notifications
 * 
 * @author makcro
 * 
 */
public class BulkDeliveryReportHandler implements HttpRequestHandler {

	public enum StarionBulkDeliveryReportParams {
		
		MSG_ID("msg_id"), STATUS("status"), DESC("desc");

		private String	name;

		private StarionBulkDeliveryReportParams(String value) {
			this.name = value;
		}

		public String getName() {
			return name;
		}
	}

	// logger instance
	private Logger				logger	= Logger.getLogger( BulkDeliveryReportHandler.class );

	/**
	 * Instance of STARION communicator used to further process delivery reports
	 */
	private StarionCommunicator	communicator;

	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// extract request parameters
		String messageId = request.getParameter( StarionBulkDeliveryReportParams.MSG_ID.getName() );
		String status = request.getParameter( StarionBulkDeliveryReportParams.STATUS.getName() );
		String description = request.getParameter( StarionBulkDeliveryReportParams.DESC.getName() );
		
		this.communicator.onBulkDeliveryReport( messageId, status, description );
		
		PrintWriter responseWriter = response.getWriter();
		responseWriter.println( "success" );
		responseWriter.close();
	}

	
	public void setCommunicator(StarionCommunicator communicator) {
		this.communicator = communicator;
	}
}
